/**
 *
 * The MIT License
 *
 * Copyright (c) 2008 Board of Trustees, Leland Stanford Jr. University. All rights reserved.
 * Copyright (c) 2009 Ben Suter. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.macdadi.ks.client.views;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import org.macdadi.core.client.auth.AuthService;
import org.macdadi.core.client.auth.AuthenticatedUser;
import org.macdadi.core.client.domain.ExtendedProjectInfo;
import org.macdadi.ks.client.View;
import org.macdadi.ks.client.ViewContainer;
import org.macdadi.ks.client.components.ValuesChart;

/**
 * @author Ben Suter <ben.suter@gmail.com>
 */
public class Welcome extends View
{
    protected HTML heading;
    protected FlexTable table;
    protected ValuesChart valuesChart;
    protected final String CHART_URL_BASE = "http://chart.apis.google.com/chart?";
    boolean allowEditProject = false;

    public static ViewInfo init()
    {
        String info = "<p>Collaborative decisions that provide value to stakeholders!</p>";

        return new ViewInfo("Welcome", info)
        {
            public View createInstance(ViewContainer container)
            {
                return new Welcome(container);
            }
        };
    }

    public Welcome(ViewContainer container)
    {
        this.container = container;

        VerticalPanel mainPanel = new VerticalPanel();
        mainPanel.setStyleName("macdadi-Welcome");
        mainPanel.setWidth("100%");

        heading = new HTML();
        mainPanel.add(heading);

        HorizontalPanel bodyPanel = new HorizontalPanel();
        mainPanel.add(bodyPanel);

        table = new FlexTable();
        bodyPanel.add(table);

        valuesChart = new ValuesChart();
        valuesChart.setChartTitle("Options ranked by Value");
        valuesChart.setShowMessageIfNotAuthorized(false);
        bodyPanel.add(valuesChart);

        initWidget(mainPanel);       
    }

    public void onShow() {
        displayProjectInfo();
        populateChart();
        
        AuthenticatedUser user = container.getCurrentUser();
        if ( user == null )
            allowEditProject = false;
        else
            allowEditProject = user.isAuthorizedOperation(container.getCurrentProject().getId(), AuthService.TARGET_STAKEHOLDERS, AuthService.OPERATION_UPDATE);
    }

    public void onHide() {
        heading.setText("");
        table.clear();
        valuesChart.clear();
        allowEditProject = false;
    }

    protected void populateChart()
    {
        valuesChart.populate(container.getCurrentProject().getId(), container.getCurrentAuthToken());
    }

    /**
     * Display information about this project and its settings.
     */
    protected void displayProjectInfo()
    {
        final ExtendedProjectInfo project = container.getCurrentProject();
        
        // TODO: This is how I want this page to look eventually, but
        // some of these settings reflect functionality that has not
        // yet been implemented at the core level

        // Heading: name of project
        // Phase: ... -> ... -> CAPS -> ... -> final
        // Create an account (only visibile if not authenticated)
        // Join the project  (only visible if not already a member)
        // Become a stakeholder (only visible if not already a stakeholder)
        // Can be viewed by anyone?         YES no
        // Anyone can join the project?     yes NO
        // Anyone can be a stakeholder?     yes NO

        // 3rd columns is "edit" link, visible for DM's only
        // other commands for DM's:
        //  TODO: "edit url" -> redirects if successful
        //  TODO: "edit color" -> reloads if successful


        heading.setHTML("<h3>" + project.getName() + "</h3>");

        int nextRow = 0;

        /* Show the phase */
        // TODO: actually show the phase
//        table.setWidget(nextRow, 0, new HTML("Phase: ..."));
//        if ( allowEditProject ) {
//           table.setWidget(nextRow, 2, new Button("Edit", new ClickListener() {
//                public void onClick(Widget sender) {
//                    // change the phase
//                }
//            }));
//        }
//        nextRow++;

         /* Let unauthenticated users display an account */
        // TODO: listen for userAuthenticated / logged out hide this option
        AuthenticatedUser user = container.getCurrentUser();
        if ( user == null ) {
            table.setWidget(nextRow, 0, new HTML("Not registered yet? <a href='index.html'>Create an account</a>"));
            table.getFlexCellFormatter().setColSpan(nextRow, 0, 2);
            nextRow++;

            table.setWidget(nextRow, 0, new HTML("<hr width='100px' />"));
            nextRow++;
        }

        table.setWidget(nextRow, 0, new HTML("Stats:"));
        nextRow++;
        table.setWidget(nextRow, 0, new HTML("# of project members:"));
        table.setWidget(nextRow, 1, new HTML("<b>" + project.getNumProjectMembers() + "</b>"));
        nextRow++;
        table.setWidget(nextRow, 0, new HTML("# of goals:"));
        table.setWidget(nextRow, 1, new HTML("<b>" + project.getNumGoals() + "</b>"));
        nextRow++;
        table.setWidget(nextRow, 0, new HTML("# of options:"));
        table.setWidget(nextRow, 1, new HTML("<b>" + project.getNumOptions() + "</b>"));
        nextRow++;

        table.setWidget(nextRow, 0, new HTML("<hr width='100px' />"));
        nextRow++;
                
        table.setWidget(nextRow, 0, new HTML("Settings:"));
        nextRow++;

        table.setWidget(nextRow, 0, new HTML("Can be viewed by anyone?"));
        String settingPublic;
        settingPublic = project.isPublic() ? "<b>yes</b>" : "<b>no</b>" ;
        table.setWidget(nextRow, 1, new HTML(settingPublic));
        nextRow++;

        table.setWidget(nextRow, 0, new HTML("Can be copied?"));
        String settingAllowCopy;
        settingAllowCopy = project.isAllowCopy() ? "<b>yes</b>" : "<b>no</b>" ;
        table.setWidget(nextRow, 1, new HTML(settingAllowCopy));
        nextRow++;


        // TODO: implement project setting: anyone_may_join
//        table.setWidget(nextRow, 0, new HTML("Anyone can join the project?"));
//        String settingAnyoneJoin;
//        settingAnyoneJoin = project.xxx() ? "<b>YES</b> no" : "yes <b>NO</b>" ;
//        table.setWidget(nextRow, 1, new HTML(settingAnyoneJoin));
//        nextRow++;

         // TODO: implement project setting: anyone_may_be_stakeholder
//        table.setWidget(nextRow, 0, new HTML("Anyone can be a stakeholder?"));
//        String settingAnyoneStakeholder;
//        settingAnyoneStakeholder= project.xxx() ? "<b>YES</b> no" : "yes <b>NO</b>" ;
//        table.setWidget(nextRow, 1, new HTML(settingAnyoneStakeholder));
//        nextRow++;

    }

}
